package com.gu.controller;


import com.gu.domain.dto.AddTagDto;
import com.gu.domain.dto.EditTagDto;
import com.gu.domain.dto.TagListDto;
import com.gu.domain.entity.ResponseResult;
import com.gu.domain.entity.Tag;
import com.gu.domain.service.TagService;
import com.gu.domain.utils.BeanCopyUtils;
import com.gu.domain.vo.PageVo;
import com.gu.domain.vo.TagVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/content/tag")
public class TagController {
    @Autowired
    private TagService tagService;


    @GetMapping("/list")
    public ResponseResult<PageVo> list(Integer pageNum, Integer pageSize, TagListDto tagListDto) {

        return tagService.pageTagList(pageNum,pageSize,tagListDto);
    }

    //新增标签功能接口
    @PostMapping
    public ResponseResult add(@RequestBody AddTagDto tagDto){
        Tag tag = BeanCopyUtils.copyBean(tagDto, Tag.class);
        return ResponseResult.okResult(tagService.save(tag));
    }


    //删除标签功能接口(逻辑删除)
    @DeleteMapping
    public ResponseResult remove(@RequestParam(value = "ids")String ids) {
        //根据id删除标签
        return tagService.removeTags(ids);
    }

    @PutMapping
    //②根据标签的id来修改标签
    public ResponseResult edit(@RequestBody EditTagDto tagDto){
        Tag tag = BeanCopyUtils.copyBean(tagDto,Tag.class);
        tagService.updateById(tag);
        return ResponseResult.okResult();
    }

    //---------------------------写博文-查询文章标签的接口---------------------------

    @GetMapping("/listAllTag")
    public ResponseResult listAllTag(){
        List<TagVo> list = tagService.listAllTag();
        return ResponseResult.okResult(list);
    }

}
